using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Edge.Common.ComponentModel;
using Edge.Common.Utils;
using Edge.Common.ComponentModel;
using Edge.Common.Utils;

namespace Edge.Common.Web.Navigation
{

    /// <summary>
    /// Summary description for SimpleNavigationService
    /// </summary>
    public class SimpleNavigationService : INavigationService
    {
        public SimpleNavigationService()
        {
        }

        public string GetCurrentQueryString(UrlBuilder uri, String[] removeParam)
        {
            UrlBuilder ub = (UrlBuilder)uri.Clone();
            foreach (string s in removeParam)
            {
                ub.QueryString.Remove(s);
            }
            return ub.ToString();
        }

        public string GetCurrentQueryString(NameValueCollection queryString, String[] removeParam)
        {
            NameValueCollection c = new NameValueCollection(queryString);
            foreach (string s in removeParam)
            {
                c.Remove(s);
            }
            String[] keys = c.AllKeys;
            String[] param = new String[keys.Length];
            int i = 0;
            foreach (string s in keys)
            {
                param.SetValue(s + "=" + c[s], i);
                i++;
            }
            return String.Join("&", param);
        }


        public virtual bool IsProvideUrl(object obj)
        {
            //            if (obj is Territory)
            //                return false;
            return true;
        }

        public string GetUrl(object obj, Action action)
        {
            if (obj == null)
                throw new ArgumentException("obj");

            object id = ReflectionUtils.GetPublicPropertyValue(obj, "Id");

            return GetUrl(obj.GetType(), id, action);

        }

        #region INavigationService Members

        public string GetUrl(Type type, object id, Action action)
        {

            if (type == null)
                throw new ArgumentException("type");



            Type entityType = type;
            entityType = RelatedTypeAttribute.GetRelatedTypeOrSelf(entityType);

            //            if (id == null)
            //                throw new ArgumentException("null id");

            string prefix = null;
            if (entityType.Name.Contains("_NHibernate_Proxy"))
            {
                prefix = entityType.BaseType.Name;
            }
            else
            {
                prefix = entityType.Name;
            }


            UrlBuilder ub = UrlBuilder.FromPage(prefix + "Details.aspx");
            if (id != null && action != Action.New)
                ub.QueryString["id"] = id.ToString();
            ub.QueryString["action"] = action.ToString();

            return ub.ToString();
        }

        public string GetListUrl(Type type)
        {
            if (type == null)
                throw new ArgumentException("type");

            return type.Name + "List.aspx";
        }

        #endregion

        public string GetCreateUrl(Type type)
        {
            if (type == null)
                throw new ArgumentException("type");
            return type.Name + "Details.aspx?action=" + Action.New.ToString();
        }
    }


}